//
//  CCNewFeatureCollectionVC.swift
//  swift_weibo
//
//  Created by ccl on 16/3/4.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CCNewFeatureCollectionVC: UICollectionViewController {

    var layout = UICollectionViewFlowLayout();
    
    init() {
        super.init(collectionViewLayout: layout);
    
    }

  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal;
        
        collectionView?.bounces = false;
        collectionView?.pagingEnabled = true;
        
        self.collectionView!.registerClass(CCNewFeaTureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        layout.itemSize = view.bounds.size;
        
       
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource
    let imageCount = 4;
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount;
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CCNewFeaTureCell;
        
        cell.imageIndex(indexPath.row);
    
        // Configure the cell
        cell.backgroundColor = UIColor.greenColor();
                return cell
    }

    // MARK: UICollectionViewDelegate
  override  func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    
     // 取出滚动停止后, 当前显示的cell的index
     let path = collectionView.indexPathsForVisibleItems().last!
    if path.row == imageCount - 1{
        let mycell = collectionView.cellForItemAtIndexPath(path) as! CCNewFeaTureCell;
        mycell.showStartBtn();
       
    }
  }
}

    
    
    
class CCNewFeaTureCell: UICollectionViewCell {
    
    var btnBottomConstraint: NSLayoutConstraint?;
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        creatUI();
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    ///  对外方法,设置imageView
    ///
    ///  - parameter index: 设置第index 个cell的图片
    func imageIndex(index: Int){
    
        imgView.image = UIImage(named: "new_feature_\(index + 1)");
        
        startBtn.hidden = true;
    }
    ///  显示btn
    func showStartBtn(){
        startBtn.hidden = false;
        startBtn.transform = CGAffineTransformMakeScale(0, 0);
        startBtn.userInteractionEnabled = false;
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity;
            
            }) { (_ ) -> Void in
             self.startBtn.userInteractionEnabled = true;
        }

    }
    
    private func creatUI () {
        addSubview(imgView);
        addSubview(startBtn);
        
        btnBottomConstraint = NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
        startBtn.translatesAutoresizingMaskIntoConstraints = false;
        addConstraint(btnBottomConstraint!);
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0));
        
    }
    
     func startBtnDidClick(sender: UIButton){
    
        UIApplication.sharedApplication().keyWindow?.rootViewController = myTabBarVC();
    }
    
   override func layoutSubviews() {
        super.layoutSubviews()
        imgView.frame = self.bounds
        
    }
   // MARK: 懒加载创建控件
    lazy var imgView: UIImageView = {
    
      return UIImageView();
    }()
    
    lazy var startBtn: UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom);
        
        btn.setTitle("开始体验", forState: UIControlState.Normal);
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        
        let image = UIImage(named: "new_feature_finish_button_highlighted");
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal);
        btn.setBackgroundImage(image, forState: UIControlState.Highlighted);
        
        btn.frame.size = image!.size;
        
        btn.addTarget(self, action: "startBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside);
        
        
        return btn;
    }()
    
    
}